package com.xxx.client.android.dialog;

import android.app.Activity;
import android.content.Intent;

import com.xxx.client.android.activity.NavigationActivity;
import com.xxx.client.android.api.NavigationEntry;
import com.xxx.client.android.api.WSError;
import com.xxx.client.android.api.XXXApi;
import com.xxx.client.android.api.impl.XXXApiImpl;

public class NavigationLoadingDialog extends
		LoadingDialog<Void, NavigationEntry> {

	public NavigationLoadingDialog(Activity activity, int loadingMsg,
			int failMsg) {
		super(activity, loadingMsg, failMsg);
	}

	@Override
	public NavigationEntry doInBackground(Void... params) {

		XXXApi service = new XXXApiImpl();
		NavigationEntry navigationEntry = null;
		try {
			navigationEntry = service.getNavigation();
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		} catch (WSError e) {
			publishProgress(e);
			cancel(true);
		}
		return navigationEntry;

	}

	@Override
	public void doStuffWithResult(NavigationEntry navigationEntry) {

		Intent intent = new Intent(mActivity, NavigationActivity.class);

		intent.putExtra("navigationEntry", navigationEntry);
		mActivity.startActivity(intent);
		if (mActivity instanceof NavigationActivity) {
			mActivity.finish();
		}
	}

}
